package com.example.print.fragment;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.print.R;
import com.example.print.activity.MainActivity;

import java.io.InputStream;

/**
 * A simple {@link Fragment} subclass.
 */
public class PrintTextFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "TextPrintActivity";

    private LinearLayout fontleft, fontcenter, fontright, smallfont, mediemfont, bigfont, fontBold, fontUnderline;
    private EditText et_text, text_Paper_distance, text_chroma;
    private Button btn_textprint;

    private boolean isBold = false; //判断文本是否加粗，默认不加粗
    private boolean isUnderline = false; //判断文本时候有下划线，默认为无

    private MainActivity mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_printtext, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = (MainActivity) getActivity();

        //获取相对应控件id
        fontleft = mContext.findViewById(R.id.fontleft);
        fontcenter = mContext.findViewById(R.id.fontcenter);
        fontright = mContext.findViewById(R.id.fontright);
        smallfont = mContext.findViewById(R.id.smallfont);
        mediemfont = mContext.findViewById(R.id.mediemfont);
        bigfont = mContext.findViewById(R.id.bigfont);
        fontBold = mContext.findViewById(R.id.fontBold);
        fontUnderline = mContext.findViewById(R.id.fontUnderline);
        et_text = mContext.findViewById(R.id.et_text);
        btn_textprint = mContext.findViewById(R.id.btn_textprint);

        //走纸距离及浓度
        text_Paper_distance = mContext.findViewById(R.id.text_Paper_distance);//走纸距离
        text_chroma = mContext.findViewById(R.id.text_chroma);//浓度

        fontleft.setOnClickListener(this);
        fontcenter.setOnClickListener(this);
        fontright.setOnClickListener(this);
        smallfont.setOnClickListener(this);
        mediemfont.setOnClickListener(this);
        bigfont.setOnClickListener(this);
        fontBold.setOnClickListener(this);
        fontUnderline.setOnClickListener(this);
        btn_textprint.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        float size = et_text.getTextSize(); //获取输入框文字大小
        switch (v.getId()) {
            //字体格式，目前只能实现选中，未实现打印机中改变格式
            case R.id.fontleft:
                fontleft.setBackgroundResource(R.drawable.text_border_style2);
                fontcenter.setBackgroundResource(R.drawable.text_border_style);
                fontright.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setPrintAlignmentMode(0);//48
                et_text.setGravity(Gravity.LEFT);
                break;
            case R.id.fontcenter:
                fontcenter.setBackgroundResource(R.drawable.text_border_style2);
                fontleft.setBackgroundResource(R.drawable.text_border_style);
                fontright.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setPrintAlignmentMode(1);//49
                et_text.setGravity(Gravity.TOP | Gravity.CENTER);
                break;
            case R.id.fontright:
                fontright.setBackgroundResource(R.drawable.text_border_style2);
                fontcenter.setBackgroundResource(R.drawable.text_border_style);
                fontleft.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setPrintAlignmentMode(2);//50
                et_text.setGravity(Gravity.RIGHT);
                break;
            //字体大小，目前只能在输入框中实现
            case R.id.smallfont:
                et_text.setTextSize(12);
                smallfont.setBackgroundResource(R.drawable.text_border_style2);
                mediemfont.setBackgroundResource(R.drawable.text_border_style);
                bigfont.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setFontDimension(0);
                break;
            case R.id.mediemfont:
                et_text.setTextSize(24);
                mediemfont.setBackgroundResource(R.drawable.text_border_style2);
                smallfont.setBackgroundResource(R.drawable.text_border_style);
                bigfont.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setFontDimension(17);//2倍高，2倍宽
                break;
            case R.id.bigfont:
                et_text.setTextSize(36);
                bigfont.setBackgroundResource(R.drawable.text_border_style2);
                smallfont.setBackgroundResource(R.drawable.text_border_style);
                mediemfont.setBackgroundResource(R.drawable.text_border_style);
                mContext.mPrinter.setFontDimension(49);//2倍高，4倍宽
                break;
            //文字加粗，目前只能在输入框中显示，不能在打印机中实现
            case R.id.fontBold:
                if (isBold) {
                    et_text.setTypeface(null, Typeface.NORMAL); // 取消加粗
                    fontBold.setBackgroundResource(R.drawable.text_border_style);
                    isBold = false;
                    mContext.mPrinter.setBoldMode(0);
                } else {
                    et_text.setTypeface(null, Typeface.BOLD); // 加粗
                    fontBold.setBackgroundResource(R.drawable.text_border_style2);
                    mContext.mPrinter.setBoldMode(1);
                    isBold = true;
                }

                break;
            //添加下划线，目前只能在输入框中显示，不能在打印机中实现
            case R.id.fontUnderline:
                if (isUnderline) {
                    et_text.setPaintFlags(et_text.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG)); // 取消下划线
                    fontUnderline.setBackgroundResource(R.drawable.text_border_style);
                    isUnderline = false;
                    mContext.mPrinter.setUnderlineMode(0);// 48
                } else {
                    et_text.setPaintFlags(et_text.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); // 添加下划线
                    fontUnderline.setBackgroundResource(R.drawable.text_border_style2);
                    mContext.mPrinter.setUnderlineMode(1);// 49
                    isUnderline = true;
                }
                break;
            //文本打印按钮
            case R.id.btn_textprint:
                Log.d(TAG, "btn_textprint....");
                if(false == mContext.IsPrinterNormal() || mContext.get_is_printing())
                {
                    Toast.makeText(mContext, R.string.PrintBusy, Toast.LENGTH_SHORT).show();
                    return;
                }
                mContext.set_is_printing(true);
                if (TextUtils.isEmpty(et_text.getText())) {
                   // printPicTest();
                    Toast.makeText(mContext, R.string.Print_enter, Toast.LENGTH_SHORT).show();
                } else {
                    int dim = 25;
                    int distance = 5;
                    try {
                        String StrTemp = text_chroma.getText().toString().trim();
                        dim = Integer.parseInt(StrTemp);
                    } catch (Exception e) {
                        text_chroma.setText(dim + "");
                    }
                    mContext.mPrinter.setPrintChromaLevel(dim);

                    String TestData = et_text.getText().toString();  // 获取输入的文字
                    new Thread() {
                        @Override
                        public void run() {
                    if (mContext.mPrinter.blacklabel_status){
                        mContext.mPrinter.printText(TestData);
                        //跳转到下一张标签同样的位置
                        mContext.mPrinter.Feed_to_Next();
                    }else {
                        mContext.mPrinter.printText(TestData);
                        //打印完走纸设置的行数
                        mContext.mPrinter.setFeedRow(0, distance);
                    }
                        }
                    }.start();
                }

                mContext.set_is_printing(false);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }

    private void printPicTest() {
        try {
            InputStream inputStream = getResources().getAssets().open("pic_base64.txt");
            byte[] buff = new byte[10240];
            int len;
            StringBuilder stringBuilder = new StringBuilder();
            while ((len = inputStream.read(buff)) != -1) {
                stringBuilder.append(new String(buff, 0, len));
            }
            inputStream.close();
            byte[] bytes = Base64.decode(stringBuilder.toString(), Base64.DEFAULT);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
            mContext.mPrinter.setPrintAlignmentMode(49);//居中
            mContext.mPrinter.printImage(bitmap);
            mContext.mPrinter.setFeedRow(0,5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
